s = '''
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip install -r requirements.txt
'''
# import xml.sax

# class parse_xml(xml.sax.ContentHandler):
#     def __init__(self):
#         self.ip = ''
#     def startElement(self,tag,attr):
#         self.currentDATA = tag
#         if 'port'
from os import system
import os
import xml.sax
ports = []
basedir = os.getcwd()
class IP_parse(xml.sax.ContentHandler):
    def startDocument(self):
        self.ip = ''
        self.port = ''
        global ports
        self.data= {
            port:list()
            for port in ports
        }
    def startElement(self,tag,attr):
        if tag == 'address':
            self.ip = attr['addr']
        if tag == 'port':
            self.port = attr['portid']
        if self.ip != '' and self.port != '':
            self.data[self.port].append(self.ip)
            self.ip = ''
            self.port = ''
    def endDocument(self):
        # print(self.data.keys())
        for fn in self.data.keys():
            with open('ips/%s.txt' % fn,'w+') as f:
                for ip in self.data[fn]:
                    f.writelines(ip+'\n')
class port_parse(xml.sax.ContentHandler):
    def startDocument(self):
        self.ports = []
    def startElement(self,tag,attr):
        if tag =='port':
            self.ports.append(attr['portid'])
    def endDocument(self):
        global ports
        ports = self.ports
def startparse():
    parser = xml.sax.make_parser()
    parser.setFeature(xml.sax.handler.feature_namespaces,0)

    handler = port_parse()
    parser.setContentHandler(handler)
    parser.parse('result.xml')
    handler = IP_parse()
    parser.setContentHandler(handler)
    parser.parse('result.xml')
def scan_script():
    global ports
    bat_sc = ['python brute.py %s 300' % port for port in set(ports)]
    txt = '\n'.join(bat_sc)
    with open('brute.sh','w') as f:
        f.writelines(txt)
def ready():
    system('rm /rf ips')
    # system('pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/')
    # system('pip install -r requirements.txt')
    system('mkdir ips')
if __name__ == "__main__":
    ready()
    system('masscan -c config.ini')
    startparse()
    scan_script()
    system('brute.sh')